package com.seuic.hnapps.util;

import com.orhanobut.logger.Logger;
import com.seuic.hnapps.bean.CardBinInfo_bean;
import com.solab.enc.DES3;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;

import java.util.List;

/**
 * 本行账号现金汇款
 * 转他行
 */
public class BRInterface {
    public IsoMessage packRequest(String bindNum, String transn, String moneys, String passWord, String bind_num,String inputname,String outjgh,String outname) {
        Logger.e(transn);
        IsoMessage msg = new IsoMessage();
        String encflag = Constants.YANTAI_ENCFLG2;
        msg.setType(0x0200);
        msg.setHeadValue(1, String.valueOf(Constants.ATMP_HEADER_LENGTH), IsoType.BCD, 2);
        msg.setHeadValue(2, Constants.YANTAI_PACKVS, IsoType.ALPHA, 2);
        msg.setHeadValue(3, Constants.YANTAI_TERMVS, IsoType.ALPHA, 8);
        msg.setHeadValue(4, encflag, IsoType.ALPHA, 1); // 加密标志：业务交易，公网传输1-报文加密，专线传输2-磁道加密
        msg.setHeadValue(5, Constants.YANTAI_BANKID, IsoType.ALPHA, 8);
        msg.setHeadValue(6, Constants.YANTAI_TERMNO, IsoType.ALPHA, 8);
        msg.setHeadValue(7, "0000", IsoType.ALPHA, 4); // 8583域部分长度，组包时动态填充

        //****************传备付金账号****************
        msg.setValue(2, Constants.DEMO_F102, IsoType.LLVAR, 19);
        msg.setValue(3, "410000", IsoType.ALPHA, 6);

        String money = "";
        String f3 = "";
        if (moneys.indexOf(".") != -1) {

            String moneylengs[] = moneys.split("\\.");
            if (moneylengs[1].length() == 2) {
                if (moneylengs[0].length() == 1) {
                    f3 = "000000000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 2) {
                    f3 = "00000000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 3) {
                    f3 = "0000000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 4) {
                    f3 = "000000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 5) {
                    f3 = "00000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 6) {
                    f3 = "0000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 7) {
                    f3 = "000" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 8) {
                    f3 = "00" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 9) {
                    f3 = "0" + moneylengs[0] + moneylengs[1];
                } else if (moneylengs[0].length() == 10) {
                    f3 = moneylengs[0] + moneylengs[1];
                }

            } else {
                money = moneys.replace(".", "");
                if (money.length() == 1) {
                    f3 = "0000000000" + money + "0";
                } else if (money.length() == 2) {
                    f3 = "000000000" + money + "0";
                } else if (money.length() == 3) {
                    f3 = "00000000" + money + "0";
                } else if (money.length() == 4) {
                    f3 = "0000000" + money + "0";
                } else if (money.length() == 5) {
                    f3 = "000000" + money + "0";
                } else if (money.length() == 6) {
                    f3 = "00000" + money + "0";
                } else if (money.length() == 7) {
                    f3 = "0000" + money + "0";
                } else if (money.length() == 8) {
                    f3 = "000" + money + "0";
                } else if (money.length() == 9) {
                    f3 = "00" + money + "0";
                } else if (money.length() == 10) {
                    f3 = "0" + money + "0";
                }
            }


        } else {
            money = moneys;
            if (money.length() == 1) {
                f3 = "000000000" + money + "00";
            } else if (money.length() == 2) {
                f3 = "00000000" + money + "00";
            } else if (money.length() == 3) {
                f3 = "0000000" + money + "00";
            } else if (money.length() == 4) {
                f3 = "000000" + money + "00";
            } else if (money.length() == 5) {
                f3 = "00000" + money + "00";
            } else if (money.length() == 6) {
                f3 = "0000" + money + "00";
            } else if (money.length() == 7) {
                f3 = "000" + money + "00";
            } else if (money.length() == 8) {
                f3 = "00" + money + "00";
            } else if (money.length() == 9) {
                f3 = "0" + money + "00";
            } else if (money.length() == 10) {
                f3 = money + "00";
            }
        }
        msg.setValue(4, f3, IsoType.ALPHA, 12);
        msg.setValue(7, DateUtil.getCurrentTime(DateUtil.M_TIME_PATTERN), IsoType.ALPHA, 10);
        msg.setValue(8, Constants.YANTAI_CHANLENO, IsoType.ALPHA, 2);
        msg.setValue(11, transn, IsoType.ALPHA, 6);

        // 第20域，需要严格按照规范填写 21121（6）00
        msg.setValue(20, "01100000", IsoType.ALPHA, 8);
        // 第22域，需要严格按照规范填写 手工输入01 磁条读取02 IC卡 05 第三位是否有密码1 无密码2

        msg.setValue(22, "022", IsoType.ALPHA, 3);

        msg.setValue(41, Constants.YANTAI_TERMNO, IsoType.ALPHA, 8);
        // 第49域，币种
        msg.setValue(49, "156", IsoType.ALPHA, 3);
        //第52域，输入备付金账号密码
        if(Constants.isSM){
            msg.setValue(53, Constants.DEMO_F53, IsoType.ALPHA, 16);
        }
        // 第57域，终端各部件状态，按实际填写
        msg.setValue(57, Constants.YANTAI_F57, IsoType.LLLVAR, 127);
        // 第60域，按实际填写
        msg.setValue(60, Constants.YANTAI_BATCHNO + "6" + "0", IsoType.LLVAR, 30);
        msg.setValue(102, Constants.DEMO_F102, IsoType.LLVAR, 28);
        //第103域，按实际填写 转出账户 他行

        msg.setValue(103, bindNum, IsoType.LLVAR, 28);
        String f122l = "01" +"|"+outjgh+"|"+outname+"|"+ bind_num+"|"+ inputname+"|";
        if (f122l.length()>10){
            msg.setValue(122, "BT"+"0"+f122l.length()+ f122l, IsoType.LLLVAR, 512);
        }else {
            msg.setValue(122, "BT"+"0"+"0"+f122l.length()+ f122l, IsoType.LLLVAR, 512);
        }

        // MAC，填写8字节，则需要生成MAC，外层统一处理；本域不填写，则外层不会生成
        msg.setValue(128, "00000000", IsoType.ALPHA, 8);

        return msg;
    }
}
